<template>
  <el-tabs v-model="activeName" class="demo-tabs" @tab-click="handleTabClick">
    <el-tab-pane label="场景配置" :name="1">
    </el-tab-pane>
    <el-tab-pane label="子场景配置" :name="2">
    </el-tab-pane>
  </el-tabs>
  <el-button plain @click="handleQuery">
    <Icon icon="ep:refresh" class="mr-5px" /> 刷新
  </el-button>
  <el-button
    type="primary"
    plain
    @click="openForm('create')"
    v-hasPermi="['service:scene-config:create']"
  >
    <Icon icon="ep:plus" class="mr-5px" /> 新增
  </el-button>
  <el-table
    v-loading="loading"
    :data="list"
    :stripe="true"
    :show-overflow-tooltip="true"
    class="mt-4"
    border
  >
    <el-table-column label="编号" align="center" type="index" width="100" />
    <el-table-column
      label="场景名称"
      align="center"
      prop="name"
    />
    <el-table-column
      label="场景备注"
      align="center"
      prop="remark"
    />
    
    <el-table-column
      label="创建时间"
      align="center"
      prop="createTime"
      :formatter="dateFormatter"
      width="180px"
    />
    <el-table-column label="操作" align="center" min-width="110" fixed="right">
      <template #default="scope">
        <el-button
          link
          type="primary"
          @click="openForm('update', scope.row)"
          v-hasPermi="['service:scene-config:update']"
        >
          编辑
        </el-button>
        <el-button
          link
          type="primary"
          @click="openStep(scope.row)"
          v-hasPermi="['service:scene-config:update']"
        >
          步骤配置
        </el-button>
        <el-button
          link
          type="danger"
          @click="handleDelete(scope.row.id)"
          v-hasPermi="['service:scene-config:delete']"
        >
          删除
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  <!-- 分页 -->
  <Pagination
    :total="total"
    v-model:page="queryParams.pageNo"
    v-model:limit="queryParams.pageSize"
    @pagination="getList"
  />
  <AddForm ref="formRef" @success="getList" />
</template>
<script setup lang="ts">
import AddForm from './AddForm.vue'
import { dateFormatter } from '@/utils/formatTime'
import * as SceneApi from '@/api/service/scene/index'
import * as CustomerLimitConfigApi from '@/api/crm/customerLimitConfig'

defineOptions({ name: 'ServiceSceneList' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const activeName = ref(1)


const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  type: activeName.value
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const handleTabClick = ({props}) => {
  queryParams.type = props.name
  getList()
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await SceneApi.getScenePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, obj?: object, sceneType: string) => {
  formRef.value.open(type, obj, activeName.value)
}

/** 配置步骤跳转操作 */
const router = useRouter()
const openStep = (query) => {
  router.push({
    name: 'ServiceSceneConfig',
    query
  })
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await SceneApi.deleteScene(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
